/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MemberPMFrame.java
 *
 * Created on Oct 17, 2011, 11:19:55 PM
 */
package teletutor.classroommanager.UI;

import teletutor.classroommanager.services.MemberProxy;
import teletutor.core.utilities.FrameUtil;
import javax.swing.SwingUtilities;
import teletutor.classroommanager.services.TextMessage;
import teletutor.classroommanager.services.TextMessageObserver;

/**
 *
 * @author Rae
 */
public class MemberPMFrame extends javax.swing.JFrame implements TextMessageObserver {

    /**
     * The proxy object for the member with whom you'd talk from this end
     */
    private MemberProxy memberProxy;

    /** Creates new form MemberPMFrame */
    public MemberPMFrame(MemberProxy memberProxy) {
        initComponents();

        this.memberProxy = memberProxy;
        memberProxy.addTMObserver(this);
        setVisible(false);
        
        setTitle(memberProxy.getLocalUser() + "-side " + memberProxy.getUsername() + "_proxy");
        FrameUtil.centerFrameOnScreen(this);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        pmHistory = new javax.swing.JTextArea();
        pmLineEdit = new javax.swing.JTextField();

        pmHistory.setColumns(20);
        pmHistory.setEditable(false);
        pmHistory.setRows(5);
        pmHistory.setFocusable(false);
        jScrollPane1.setViewportView(pmHistory);

        pmLineEdit.setText("Enter your message here:");
        pmLineEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pmLineEditActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(pmLineEdit, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pmLineEdit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void pmLineEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pmLineEditActionPerformed
        String msg = pmLineEdit.getText();
        pmLineEdit.setText("");

        memberProxy.sendPM(msg);
        pmHistory.append(new TextMessage(memberProxy.getLocalUser(), msg).toString());
    }//GEN-LAST:event_pmLineEditActionPerformed

    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                new MemberPMFrame().setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea pmHistory;
    private javax.swing.JTextField pmLineEdit;
    // End of variables declaration//GEN-END:variables

    @Override
    public void newTextMessage(TextMessage message) {
        // TODO if the window not visible, make some sound or show something
        // somewhere
        if (!this.isVisible()) {
            SwingUtilities.invokeLater(showCommand);
        }
        pmHistory.append(message.toString());
    }
    
    /**
     * A few useful GUI commands
     */
    public Runnable hideCommand = new Runnable() {
        @Override
        public void run() {
            setVisible(false);
        }
    };
    public Runnable showCommand = new Runnable() {
        @Override
        public void run() {
            setVisible(true);
            toFront();
            repaint();
        }
    };
}
